﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using GameStateManagement.Mine;

namespace GameStateManagement.Screens.Inherit
{
    class StartupScreen : GameScreen
    {
        #region Fields

        ContentManager content;
        Texture2D videoTextture;
        Video Video;
        VideoPlayer Player;
        private Vector2 position;
        private SpriteFont Font;
        private Rectangle fullscreen;
        private Song music;
        #endregion

        #region Initialization
               
        
        
        public StartupScreen()
        {
            Player = new VideoPlayer();
            TransitionOnTime = TimeSpan.FromSeconds(.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            PlayerIndex playerIndex;
            if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.Space, null, out playerIndex))
            {
                Player.Stop();
            }
        }
        
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            Video = content.Load<Video>("Video\\Comp 2");
            Font = content.Load<SpriteFont>("Tip");
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            position = new Vector2(viewport.X + 20, viewport.Y + viewport.Height - 30);
            music = content.Load<Song>("Sound\\Moongrains _ Laura Shigihara");
            fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            Player.Play(this.Video);
            MediaPlayer.Play(music);
        }


        public override void UnloadContent()
        {            
            content.Unload();
        }


        #endregion

        #region Update and Draw


        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);
            if (Player.State == MediaState.Stopped)
            {

                LoadingScreen.Load(ScreenManager, true, 0, new BackgroundScreen(),
                                   new MyMainScreen());
            }

        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;            

            if (Player.State != MediaState.Stopped)
                videoTextture = Player.GetTexture();
            spriteBatch.Begin();

            spriteBatch.Draw(videoTextture, fullscreen, Color.White);
            spriteBatch.DrawString(Font, "Press Space to skip", position, Color.White);
            spriteBatch.End();
        }


        #endregion

    }
}
